/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mh.bean;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import mh.bol.AdminStatisticBO;
import mh.entity.StatisticEn;

/**
 *
 * @author VIETNH
 */
@ManagedBean
@RequestScoped
public class AdminStatisticRequest {

    private int dayStart;
    private int monthStart;
    private int yearStart;
    private int dayEnd;
    private int monthEnd;
    private int yearEnd;
    private ArrayList<StatisticEn> listStatistic = new ArrayList<StatisticEn>();
    AdminStatisticBO asBO = new AdminStatisticBO();

    public void checkSession() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ValueBinding binding = facesContext.getApplication().createValueBinding("#{loginSession}");
        LoginSession loginSession = (LoginSession) binding.getValue(facesContext);
        if (loginSession.getEmpEn() != null) {
            if (loginSession.getEmpEn().getEtype() == 3) {
                redirect("index.xhtml");
            }
        } else {
            redirect("Login.xhtml");
        }
    }

    public void redirect(String page) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(page);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public String viewStatistic(String viewType) {
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date dStart = null;
        Date dEnd = null;
        if (viewType.equals("2")) {
            dStart = new Date(yearStart - 1900, monthStart - 1, dayStart);
            dEnd = new Date(yearStart - 1900, monthStart - 1, dayStart + 1);
        } else if (viewType.equals("3")) {
            dStart = new Date(yearStart - 1900, monthStart - 1, dayStart);
            dEnd = new Date(yearEnd - 1900, monthEnd - 1, dayEnd);
        }

        listStatistic = asBO.getStatisticBySince(viewType, dateFormat.format(dStart), dateFormat.format(dEnd));

        return "AdViewStatistic.xhtml";
    }

    public String viewStatisticByTypeComp() {
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
//        String cType = requestMap.get("cType").toString();
//        System.out.println("C_Type : " + cType);
//        listStatistic = asBO.getStatisticByTypeComp(cType);
//        return "AdDetailStatistic.xhtml";
        return "error.xhtml";
    }

    public AdminStatisticBO getAsBO() {
        return asBO;
    }

    public void setAsBO(AdminStatisticBO asBO) {
        this.asBO = asBO;
    }

    public int getDayEnd() {
        return dayEnd;
    }

    public void setDayEnd(int dayEnd) {
        this.dayEnd = dayEnd;
    }

    public int getDayStart() {
        return dayStart;
    }

    public void setDayStart(int dayStart) {
        this.dayStart = dayStart;
    }

    public ArrayList<StatisticEn> getListStatistic() {
        return listStatistic;
    }

    public void setListStatistic(ArrayList<StatisticEn> listStatistic) {
        this.listStatistic = listStatistic;
    }

    public int getMonthEnd() {
        return monthEnd;
    }

    public void setMonthEnd(int monthEnd) {
        this.monthEnd = monthEnd;
    }

    public int getMonthStart() {
        return monthStart;
    }

    public void setMonthStart(int monthStart) {
        this.monthStart = monthStart;
    }

    public int getYearEnd() {
        return yearEnd;
    }

    public void setYearEnd(int yearEnd) {
        this.yearEnd = yearEnd;
    }

    public int getYearStart() {
        return yearStart;
    }

    public void setYearStart(int yearStart) {
        this.yearStart = yearStart;
    }

    /** Creates a new instance of AdminStatisticRequest */
    public AdminStatisticRequest() {
        checkSession();
    }
}
